<?php
class Page
{
    private $total;   //数据表中总行数
    private $listRows;  //每页显示行数，就是从数据库中每一次取出的条数
    private $limit;     //private $limit = "limit 0, 10"; 封装到类中，不要写死, 这个limit本来就是动态的
    private $url;
    private $pageNum; //分的页数
    private $config = array("header" => "商品", "prev" => "上一页", "next" => "下一页", "first" => "首页", "last" => "尾页");
   // private $page;
    private $listNum = 5; //列表的数目
    
    public function __construct($total, $listRows = 10)
    {
        $this->total = $total;
        $this->listRows = $listRows;
        $this->url = $this->getUrl();

        //$this->page,构造函数新添加的成员变量
        $this->page = !empty($_GET["page"]) ? $_GET["page"] : 1; //不为空返回，直接用收到的page,为空的话，获得了当前页
 	    $this->pageNum = ceil($this->total / $this->listRows);  //计算总的页数
 	    $this->limit = $this->setLimit();  //通过一个私有方法去设置每一次从数据中应该取出的条数
 	    //var_dump($this);   //打印出当前对像的相应的成员变量
    }

    private function setLimit()
    {  
    	return "limit ".($this->page-1)*$this->listRows.", {$this->listRows}";
    }

    private function getUrl()
    {
    	$url = $_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"], "?") ? " ":"?");
    	$parse = parse_url($url);
    	//print_r($parse);
    	if (isset($parse["query"]))
    	{
    		parse_str($parse["query"], $params);
    		//print_r($params);
    		unset($params["page"]);   //因为page是变化的，将其删掉，以重新变化
    		$url = $parse["path"]."?".http_build_query($params);
    		//echo $url;
    		//如：/my_web/suning_competition/goods_manage/class/demo.php?as=dxvx&you=nkvbcn+，没有了page=
    	}
    	return $url;
    }

    public function __get($args)   // limit 是私有的成员，当在类外使用limit时，自动调用__get()方法，完成任务，如:
    {                              //$page->limit,自动调用__get 方法
    	if ($args == "limit")
    	{
    		return $this->limit;
    	}
    	else
    	{
    		return null;
    	}
    }

    private function start()
    {
        if ($this->total == 0)
        {
            return 0;
        }
        else
        {
            //比如说现在是50页，前面的记录有49*$this->listRows,
            //再加一的话，就是当前页的第一条记录
            return ($this->page-1) * $this->listRows + 1;
        }
    }

    private function end()
    {
        //min()，获取最小值
        return min($this->page*$this->listRows, $this->total);
    }

//相应的一些显示的东东    
    private function first()   //第一页
    {
        if ($this->page == 1)
        {
            $html = " ";
        }
        else
        {
            $html = "<li><a href='{$this->url}&page=1'>{$this->config['first']}</a></li>";
        }
        return $html;
    }
    private function prev()    //前一页
    {
        if ($this->page == 1)
        {
            $html = " "; //如果是第一页的话，就没有上一页了
        }
        else
        {
            $html = "<li><a href='{$this->url}&page=".($this->page-1)."'>&lt;&lt;</a></li>";
        }
        return $html;
    }
    private function pageList()    //页的列表，1、2、3、4、5……
    {
        $linkPage = "";
        $inum = floor($this->listNum / 2);

        for ($i = $inum; $i >= 1; $i--)
        {
            $page = $this->page - $i;
            if ($page < 1)
            {
                continue;
            }
            $linkPage .= "<li><a href='{$this->url}&page={$page}'>{$page}</a></li>";      
        }
        $linkPage .= "<li><a href=\"javascript:void(0)\">{$this->page}</a></li>";
        for ($i = 1; $i <= $inum; $i++)
        {
            $page = $this->page + $i;
            if ($page <= $this->pageNum)
            {
                $linkPage .= "<li><a href='{$this->url}&page={$page}'>{$page}</a></li>";
            }
            else
            {
                break;
            }
                
        }
        return $linkPage;
    }
    private function next()   //下一页
    {
        if ($this->page == $this->pageNum)
        {
            $html = " "; //如果是最后一页，就没有下一页
        }
        else
        {
            $html = "<li><a href='{$this->url}&page=".($this->page+1)."'>&gt;&gt;</a></li>";
        }
        return $html;
    }
    private function last()    //最后一页
    {
        if ($this->page == $this->pageNum)
        {
            $html = " "; 
        }
        else
        {
            $html = "<li><a href='{$this->url}&page={$this->pageNum}'>{$this->config['last']}</a></li>";
        }
        return $html;
    }

    private function goPage()
    {        
        return '&nbsp;<input type="text" class="input-mini" id="jiao" placeholder="跳转" onkeydown="javascript:
        if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.
        ':this.value;location=\''.$this->url.'&page=\'+page;}"><button type="button" class="btn" id="bu" onclick="javascript:
         var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->url.'&page=\'+page;">Go</button>';
    }

    function fpage($display = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
    {
    	$html[0] = "&nbsp;&nbsp;共有&nbsp;<b>{$this->total}&nbsp;</b>件{$this->config['header']}&nbsp;&nbsp;";
    	$html[1] = "当前页显示&nbsp;<b>".($this->end()-$this->start()+1)."</b>&nbsp;件&nbsp;&nbsp;";
        $html[2] = "本页&nbsp;<b>{$this->start()}</b>&nbsp;到&nbsp;<b>{$this->end()}</b>&nbsp;件&nbsp;&nbsp;";
        $html[3] = "当前页&nbsp;<b>{$this->page}</b>&nbsp;/共有&nbsp;<b>{$this->pageNum}</b>&nbsp;页";

        $html[4] = $this->first();  //第一页
        $html[5] = $this->prev();    //前一页
        $html[6] = $this->pageList();  //列表
        $html[7] = $this->next();     //下一页
        $html[8] = $this->last();    //最后一页
        $html[9] = $this->goPage();  //跳转到某一页
        
        $fpage = "";
        foreach ($display as $index)
        {
            $fpage .= $html[$index];
        }

        return $fpage;
    }



}
?>

